package org.melanesia.beans;

import java.lang.reflect.Method;

import org.melanesia.beans.exceptions.PropertySetException;


public class MethodGetter implements Getter {

	private Method method;
    
    MethodGetter(Method method) {
        this.method = method;
    }
    
    @Override
    public Object get(Object bean) {
        boolean accessible = method.isAccessible();
        
        try {
            if (!accessible) {
                BeanUtils.setAccessible(method, true);
            }
            return method.invoke(bean);
            
        } catch (Throwable t) {
            throw new PropertySetException("Cannot get value using method " + method.getName(), t);
        } finally {
            BeanUtils.setAccessible(method, accessible);
        }
    }

    @Override
    public Class<?> getReturnType() {
        return method.getReturnType();
    }
    
}
